var ApprovalProcess = { // 审核  初始化
    id: "ApprovalProcessTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

ApprovalProcess.initColumn = function () { // 初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', width: 120, sortable: true},
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', width: 120, sortable: true},
        {title: '提交人', field: 'submitter', align: 'center', valign: 'middle', width: 120, sortable: true},
        {
            title: '提交时间', field: 'operationTime', align: 'center', valign: 'middle', width: 170, sortable: true,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '原建档人', field: 'name', visible: false, align: 'center', valign: 'middle', width: 110, sortable: true},
        {title: '新建档人', field: 'user', visible: false, align: 'center', valign: 'middle', width: 110, sortable: true},
        {
            title: '原跟进人',
            field: 'follower',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 110,
            sortable: true
        },
        {
            title: '新跟进人',
            field: 'xfollower',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 110,
            sortable: true
        },
        {
            title: '原归属人',
            field: 'belonger',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 110,
            sortable: true
        },
        {
            title: '新归属人',
            field: 'xbelonger',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 110,
            sortable: true
        },
        {title: '原一级渠道', field: 'sOneChannel', align: 'center', valign: 'middle', width: 120, sortable: true},
        {
            title: '原二级渠道',
            field: 'sTwoChannel',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 120,
            sortable: true
        },
        {
            title: '原三级渠道',
            field: 'sThrChannel',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 120,
            sortable: true
        },
        {title: '新一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', width: 120, sortable: true},
        {
            title: '新二级渠道',
            field: 'twoChannel',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 120,
            sortable: true
        },
        {
            title: '新三级渠道',
            field: 'thrChannel',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: 120,
            sortable: true
        },
        {title: '门店', field: 'outpatientName', align: 'center', valign: 'middle', width: 100, sortable: true},
        {
            title: '审核结果', field: 'result', align: 'center', valign: 'middle', width: 120, sortable: true,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "未完成";
                } else if (value == 1) {
                    return "同意"
                } else if (value == 2) {
                    return "不同意"
                }
            }
        },
        {
            title: '审核时间', field: 'time', align: 'center', valign: 'middle', width: 170, sortable: true,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', width: 100, sortable: true},
        {title: '当前级数', field: 'channelAuditLevel', visible: false, align: 'center', valign: 'middle', width: 100, sortable: true},
        {
            title: '操作', field: 'id', align: 'center', valign: 'middle', width: 130, sortable: true,
            formatter: function (value, row, index) {
                var ids = row.id + "," + row.reservationsId;
                if (document.getElementById("exeValue").value == "exe") {
                    if (row.channelAuditLevel == document.getElementById("channelAuditLevel").value) {
                        return '<span onclick="ApprovalProcess.modify(' + "'" + ids + "'" + ')" style="font-size: 14px;font-weight: bold;cursor: pointer">【审核】</span>'
                    } else {
                        return '<span onclick="ApprovalProcess.modifysj(' + "'" + ids + "'" + ')" style="font-size: 14px;font-weight: bold;cursor: pointer">【待审核】</span>'
                    }
                } else {
                    return '<span onclick="ApprovalProcess.modifys(' + "'" + ids + "'" + ')" style="font-size: 14px;font-weight: bold;cursor: pointer">【已审核】</span>'
                }
            }
        }
    ]
    return columns;
};

ApprovalProcess.search = function () { // 查询 列表
    ApprovalProcess.table.refresh({query: ApprovalProcess.formParams()});
};

ApprovalProcess.resetSearch = function () { // 重置
    $("#outpatientName").val("");
    $("#result").val("");
    $("#startTime").val("");
    $("#endTime").val("");
    $("#examineStartTime").val("");
    $("#examineEndTime").val("");
    ApprovalProcess.search();
};

ApprovalProcess.modify = function (ids) {
    var index = layer.open({
        type: 2,
        title: '修改渠道',
        area: ['80%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_modify/' + ids
    });
    this.layerIndex = index;
};

$("#marketSearchAll", parent.document).click(function () { // 主页搜索
    ApprovalProcess.search();
});

ApprovalProcess.modifys = function (ids) {
    var index = layer.open({
        type: 2,
        title: '修改渠道',
        area: ['80%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_modifys/' + ids
    });
    this.layerIndex = index;
};

ApprovalProcess.modifysj = function (ids) {
    var index = layer.open({
        type: 2,
        title: '修改渠道',
        area: ['80%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_modifyj/' + ids
    });
    this.layerIndex = index;
};

ApprovalProcess.getArchiving = function () { //选择建档人
    var index = layer.open({
        type: 2,
        title: '选择建档人',
        area: ['70%', '60%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
ApprovalProcess.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#marketInputPublic", parent.document).val();
    queryData['outpatientName'] = $("#outpatientName").val();  //门诊
    queryData['result'] = $("#result").val();  //审核结果
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['examineStartTime'] = $("#examineStartTime").val();
    queryData['examineEndTime'] = $("#examineEndTime").val();
    queryData['exeValue'] = $("#exeValue").val();
    return queryData;
}

/**
 * 根据点击上面状态刷新表格
 */
ApprovalProcess.approvaling = function (obj) {
    $("#examineTime").hide();
    $("#startTime").val("");
    $("#endTime").val("");
    $("#examineStartTime").val("");
    $("#examineEndTime").val("");
    $("#exeValue").val(obj);
    $('#ApprovalProcessTable').bootstrapTable('hideColumn', 'time');
    ApprovalProcess.search();
};

ApprovalProcess.approvaled = function (obj) {
    $("#examineTime").show();
    $("#exeValue").val(obj);
    $('#ApprovalProcessTable').bootstrapTable('showColumn', 'time');
    document.getElementById("examineStartTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("examineEndTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    ApprovalProcess.search();
};

$(function () {
    $("#approvaling").addClass('actionClick').siblings().removeClass('actionClick');
    document.getElementById("exeValue").value = "exe";
    var defaultColunms = ApprovalProcess.initColumn();
    var table = new BSTable(ApprovalProcess.id, "/approvalProcess/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(ApprovalProcess.formParams());
    ApprovalProcess.table = table.init();
    ApprovalProcess.search();
    ApprovalProcess.approvaling('exe');
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {ApprovalProcess.search();});
    checkDate = function () {ApprovalProcess.search();};
    callbackTable = function () {ApprovalProcess.search();};
});